package club.banyuan.request;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author edz
 * @version 1.0
 * @date 2020/12/19 上午9:49
 */
public class Request {
    private String url;//路径
    private String host;//主机名
    private int contentLength=0;//数据长度
    private String data;//数据


    /**
     * 将浏览器的输入流转换为Request对象，获取里面的信息
     */
    public Request(BufferedReader br) throws IOException {
        String line;
        while ((line = br.readLine()).length() != 0) {
            //请求行：Request Method请求方法 请求URL HTTP协议及版本
            //取得url
            if (line.startsWith("POST") || line.startsWith("GET")) {
                url = line.split(" ")[1];
            }
            //取得Host的内容
            if (line.startsWith("Host: ")) {
                host = line.replace("Host: ", "");
            }
            //取得数据长度
            if (line.startsWith("Content-Length: ")) {
                contentLength = Integer.parseInt(line.replace("Content-Length: ", ""));
            }

        }
            if (contentLength!=0) {
                char[] chars = new char[contentLength];
                int len = br.read(chars);
                data = new String(chars, 0, len);
            }
    }

    public Request() {
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getContentLength() {
        return contentLength;
    }

    public void setContentLength(int contentLength) {
        this.contentLength = contentLength;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
